package other;



import java.util.*;

public class LoadBalancerTest {



    public static void main(String[] args) throws InterruptedException {
        System.out.println("LoadBalancerTest start");

        double random;

        // List<Double> mockWeights = new ArrayList<>();
        Double[] mockWeights = new Double[]{ 0.1,0.3,0.5,0.5,0.7,1d};
        int n = 0;
//        while (n < 6)
//        {
//            mockWeights.add(ThreadLocalRandom.current().nextDouble(0.0D, 1.0D));
//            n++;
//        }
//
        Map<Integer,Integer> counter = new HashMap<>();
        Double tmp;
        while (n <100)
        {
            random= Math.random();
            int index = Arrays.binarySearch(mockWeights, random);
            index = -index - 1;

           if( !(index >= 0 && index < mockWeights.length && random < mockWeights[index]))
           {
               index = mockWeights.length-1;
           }

            tmp = mockWeights[index];
            if(counter.containsKey(index))
            {
                counter.put(index, counter.get(index) + 1);
            }
            else {
                counter.put(index, 1);
            }
            // System.out.println("random:" + String.valueOf(random));
            n++;
            Thread.sleep(50);
        }
        System.out.println("counter:");
        System.out.println(counter);


//        System.out.println("mockWeights:");
//        System.out.println(mockWeights);
//
//        System.out.println("index:" + String.valueOf(index));
//
//        System.out.println("actually index:" + String.valueOf(index) + ", value:"+ String.valueOf(mockWeights.get(index)));

        System.out.println("LoadBalancerTest end");
    }
}
